package wensheng.stu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wensheng.stu.common.R;
import wensheng.stu.dto.SetmealDto;
import wensheng.stu.entity.Category;
import wensheng.stu.entity.Setmeal;
import wensheng.stu.service.CategoryService;
import wensheng.stu.service.SetMealService;
import wensheng.stu.service.test.SetmealDishService;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setMealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setMealService.updateWithDish(setmealDto);

        return R.success("修改成功");
    }


    @GetMapping("/page")
    public R<Page> page(int page , int pageSize , String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setMealService.page(setmealPage,queryWrapper);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> collect = records.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }

            return setmealDto;
        }).collect(Collectors.toList());


        setmealDtoPage.setRecords(collect);

        return R.success(setmealDtoPage);

    }
    

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        log.info("ids: {}",ids);
        setMealService.removeWithDish(ids);

        return R.success("套餐删除成功");
    }
    @PostMapping("/status/{st}")
    public R<String> stop(@PathVariable int st , @RequestParam List<Long> ids){

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);

        List<Setmeal> list = setMealService.list(queryWrapper);

        List<Setmeal> collect = list.stream().map((item) -> {
            item.setStatus(st);
            return item;
        }).collect(Collectors.toList());

        setMealService.updateBatchById(collect);

        return R.success("售卖状态修改成功");

    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){


        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setMealService.list(queryWrapper);
        return R.success(list);
    }


    @GetMapping("/{id}")
    public R<SetmealDto> reShow(@PathVariable Long id){

        SetmealDto byIdWithDish = setMealService.getByIdWithDish(id);
        return R.success(byIdWithDish);
    }




    
}
